package enumandlambda;

/**
 * @author shkstart
 * @create 2022-12-06-14:46
 */
public class WaitDemo3 {
    public static void main(String[] args) {

        Object lock = new Object();
        Object lock2 = new Object();
        Thread t1 = new Thread(() -> {
            System.out.println("线程1 开始执行");
            synchronized (lock) {
                System.out.println("线程1  调用wait方法...");
                try {
//                    无限等待
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程1 执行完成");
            }
        }, "线程1");


        Thread t2 = new Thread(() -> {
            System.out.println("线程2 开始执行");
            synchronized (lock) {
                System.out.println("线程2  调用wait方法...");
                try {
//                    无限等待
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程2 执行完成");
            }
        }, "线程2");


        Thread t3 = new Thread(() -> {
            System.out.println("线程3 开始执行");

            synchronized (lock) {
                System.out.println("线程3  调用wait方法...");
//                    无限等待
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("线程3 执行完成");

        }, "线程3");

        t1.start();
        t2.start();
        t3.start();

        Thread t4 = new Thread(() -> {
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程4：开始执行，唤醒线程");
            synchronized (lock) {
                //发出唤醒通知
                lock.notifyAll();
                System.out.println("线程4：执行了唤醒操作");
            }
        }, "线程4 ");
        t4.start();


    }
}
